package com.example.myys.network.parse

import com.alibaba.fastjson2.JSON
import com.alibaba.fastjson2.JSONObject
import com.example.myys.Constants
import com.example.myys.data.entity.DataBean
import com.example.myys.network.HttpClient

class SyParse {
    val urlys =
        "https://biz.quark.cn/api/trending/ranking/getYingshiRanking?area=%E5%85%A8%E9%83%A8&year=%E5%85%A8%E9%83%A8&channel=%E7%94%B5%E5%BD%B1&rank_type=%E6%9C%80%E7%83%AD&cate=%E5%85%A8%E9%83%A8&from=hot_page&uc_param_str=dnfrpfbivessbtbmnilauputogpintnwmtsvcppcprsnnnchmicckpgixsnx"
    val urlDsj =
        "https://biz.quark.cn/api/trending/ranking/getYingshiRanking?area=%E5%85%A8%E9%83%A8&year=%E5%85%A8%E9%83%A8&channel=%E7%94%B5%E8%A7%86%E5%89%A7&rank_type=%E6%9C%80%E7%83%AD&cate=%E5%85%A8%E9%83%A8&from=hot_page&uc_param_str=dnfrpfbivessbtbmnilauputogpintnwmtsvcppcprsnnnchmicckpgixsnx"
    val urlDm =
        "https://biz.quark.cn/api/trending/ranking/getYingshiRanking?area=%E5%85%A8%E9%83%A8&year=%E5%85%A8%E9%83%A8&channel=%E5%8A%A8%E6%BC%AB&rank_type=%E6%9C%80%E7%83%AD&cate=%E5%85%A8%E9%83%A8&from=hot_page&uc_param_str=dnfrpfbivessbtbmnilauputogpintnwmtsvcppcprsnnnchmicckpgixsnx"
    val urlZy =
        "https://biz.quark.cn/api/trending/ranking/getYingshiRanking?area=%E5%85%A8%E9%83%A8&year=%E5%85%A8%E9%83%A8&channel=%E7%BB%BC%E8%89%BA&rank_type=%E6%9C%80%E7%83%AD&cate=%E5%85%A8%E9%83%A8&from=hot_page&uc_param_str=dnfrpfbivessbtbmnilauputogpintnwmtsvcppcprsnnnchmicckpgixsnx"
    val urlDj =
        "https://biz.quark.cn/api/trending/ranking/getYingshiRanking?channel=%E7%9F%AD%E5%89%A7&rank_type=%E6%9C%80%E7%83%AD&second_tag=true&start=0&hit=50&cate=%E5%85%A8%E9%83%A8&uc_param_str=dnfrpfbivessbtbmnilauputogpintnwmtsvcppcprsnnnchmicckpgixsnx&from=hot_page"
    val urlXs =
        "https://biz.quark.cn/api/trending/ranking/getNovelRanking?gender=male&tag=%E5%85%A8%E9%83%A8&type=rank_hot&from=hot_page&uc_param_str=dnfrpfbivessbtbmnilauputogpintnwmtsvcppcprsnnnchmicckpgixsnx"

    suspend fun getYsPh(): List<DataBean> {
        val ysList = ArrayList<DataBean>()
        val ysjson = HttpClient.get(urlys, mapOf())
        val dataJsons = JSON.parseObject(ysjson)
            .getJSONObject("data")
            .getJSONObject("hits")
            .getJSONObject("hit")
            .getJSONArray("item")
        for ((index, itemO) in dataJsons.withIndex()) {
            val dataBean = DataBean()
            val itemJSON = JSONObject.from(itemO)
            dataBean.id = index + 1
            dataBean.name = itemJSON.getString("title") ?: ""
            dataBean.author = itemJSON.getString("actors") ?: ""
            dataBean.type = Constants.YS_DY
            dataBean.cover = itemJSON.getString("src") ?: ""
            dataBean.releaseTime = itemJSON.getString("pub_date") ?: ""
            dataBean.desc = itemJSON.getString("desc") ?: ""
            dataBean.category =
                itemJSON.getString("area") ?: "" + "/" + itemJSON.getString("category") ?: ""
            ysList.add(dataBean)
        }

        return ysList
    }

    suspend fun getDsjPh(): List<DataBean> {
        val ysList = ArrayList<DataBean>()
        val ysjson = HttpClient.get(urlDsj, mapOf())
        val dataJsons = JSON.parseObject(ysjson)
            .getJSONObject("data")
            .getJSONObject("hits")
            .getJSONObject("hit")
            .getJSONArray("item")
        for ((index, itemO) in dataJsons.withIndex()) {
            val dataBean = DataBean()
            val itemJSON = JSONObject.from(itemO)
            dataBean.id = index + 1
            dataBean.name = itemJSON.getString("title") ?: ""
            dataBean.author = itemJSON.getString("actors") ?: ""
            dataBean.type = Constants.YS_DSJ
            dataBean.cover = itemJSON.getString("src") ?: ""
            dataBean.releaseTime = itemJSON.getString("pub_date") ?: ""
            dataBean.desc = itemJSON.getString("desc") ?: ""
            dataBean.category =
                itemJSON.getString("area") ?: "" + "/" + itemJSON.getString("category") ?: ""
            ysList.add(dataBean)
        }

        return ysList
    }

    suspend fun getDmPh(): List<DataBean> {
        val ysList = ArrayList<DataBean>()
        val ysjson = HttpClient.get(urlDm, mapOf())
        val dataJsons = JSON.parseObject(ysjson)
            .getJSONObject("data")
            .getJSONObject("hits")
            .getJSONObject("hit")
            .getJSONArray("item")
        for ((index, itemO) in dataJsons.withIndex()) {
            val dataBean = DataBean()
            val itemJSON = JSONObject.from(itemO)
            dataBean.id = index + 1
            dataBean.name = itemJSON.getString("title") ?: ""
            dataBean.author = itemJSON.getString("actors") ?: ""
            dataBean.type = Constants.YS_DM
            dataBean.cover = itemJSON.getString("src") ?: ""
            dataBean.releaseTime = itemJSON.getString("pub_date") ?: ""
            dataBean.desc = itemJSON.getString("desc") ?: ""
            dataBean.category =
                itemJSON.getString("area") ?: "" + "/" + itemJSON.getString("category") ?: ""
            ysList.add(dataBean)
        }

        return ysList
    }

    suspend fun getZyPh(): List<DataBean> {
        val ysList = ArrayList<DataBean>()
        val ysjson = HttpClient.get(urlZy, mapOf())
        val dataJsons = JSON.parseObject(ysjson)
            .getJSONObject("data")
            .getJSONObject("hits")
            .getJSONObject("hit")
            .getJSONArray("item")
        for ((index, itemO) in dataJsons.withIndex()) {
            val dataBean = DataBean()
            val itemJSON = JSONObject.from(itemO)
            dataBean.id = index + 1
            dataBean.name = itemJSON.getString("title") ?: ""
            dataBean.author = itemJSON.getString("actors") ?: ""
            dataBean.type = Constants.YS_ZY
            dataBean.cover = itemJSON.getString("src") ?: ""
            dataBean.releaseTime = itemJSON.getString("pub_date") ?: ""
            dataBean.desc = itemJSON.getString("desc") ?: ""
            dataBean.category =
                itemJSON.getString("area") ?: "" + "/" + itemJSON.getString("category") ?: ""
            ysList.add(dataBean)
        }

        return ysList
    }

    suspend fun getDjPh(): List<DataBean> {
        val ysList = ArrayList<DataBean>()
        val ysjson = HttpClient.get(urlDj, mapOf())
        val dataJsons = JSON.parseObject(ysjson)
            .getJSONObject("data")
            .getJSONObject("hits")
            .getJSONObject("hit")
            .getJSONArray("item")
        for ((index, itemO) in dataJsons.withIndex()) {
            val dataBean = DataBean()
            val itemJSON = JSONObject.from(itemO)
            dataBean.id = index + 1
            dataBean.name = itemJSON.getString("title") ?: ""
            dataBean.author = itemJSON.getString("actors") ?: ""
            dataBean.type = Constants.YS_DJ
            dataBean.cover = itemJSON.getString("src") ?: ""
            dataBean.releaseTime = itemJSON.getString("year") ?: ""
            dataBean.desc = itemJSON.getString("desc") ?: ""
            dataBean.category =
                itemJSON.getString("area") ?: "" + "/" + itemJSON.getString("category") ?: ""
            ysList.add(dataBean)
        }

        return ysList
    }

    suspend fun getXsPh(): List<DataBean> {
        val ysList = ArrayList<DataBean>()
        val ysjson = HttpClient.get(urlXs, mapOf())
        val dataJsons = JSON.parseObject(ysjson)
            .getJSONObject("data")
            .getJSONArray("novelList")
        for ((index, itemO) in dataJsons.withIndex()) {
            val dataBean = DataBean()
            val itemJSON = JSONObject.from(itemO)
            dataBean.id = index + 1
            dataBean.name = itemJSON.getString("title") ?: ""
            dataBean.author = itemJSON.getString("author") ?: ""
            dataBean.type = Constants.XS
            dataBean.cover = itemJSON.getString("pic") ?: ""
            dataBean.releaseTime = itemJSON.getString("status") ?: ""
            dataBean.desc = itemJSON.getString("desc") ?: ""
            dataBean.category =
                itemJSON.getString("cat") ?: "" + "/" + itemJSON.getString("tag") ?: ""
            ysList.add(dataBean)
        }

        return ysList
    }
}